home *** CD-ROM | disk | FTP | other *** search
/ Chip 2005 June / ccd0605.iso / Software / Freeware / Programare / highlight / highlight-W32GUI-2.2-10b-Setup.exe / {app} / src / stylecolour.cpp < prev    next >
C/C++ Source or Header  |  2004-10-21  |  4KB  |  162 lines

  1. /***************************************************************************
  2.                           stylecolour.cpp  -  description
  3.                              -------------------
  4.     begin                : Die Nov 5 2002
  5.     copyright            : (C) 2002 by AndrΘ Simon
  6.     email                : andre.simon1@gmx.de
  7.  ***************************************************************************/
  8.  
  9. /***************************************************************************
  10.  *                                                                         *
  11.  *   This program is free software; you can redistribute it and/or modify  *
  12.  *   it under the terms of the GNU General Public License as published by  *
  13.  *   the Free Software Foundation; either version 2 of the License, or     *
  14.  *   (at your option) any later version.                                   *
  15.  *                                                                         *
  16.  ***************************************************************************/
  17.  
  18. #include "stylecolour.h"
  19.  
  20. using std::string;
  21.  
  22. namespace highlight {
  23.  
  24. StyleColour::StyleColour(const string & r_hex, const string & g_hex, const string & b_hex)
  25.     : r(r_hex), g(g_hex), b(b_hex)
  26. {}
  27. StyleColour::StyleColour()
  28.     : r("00"), g("00"), b("00")
  29. {}
  30.  
  31. //Parst PArameter aus style-Datei
  32. StyleColour::StyleColour(const string & styleColourString)
  33. {
  34.   setRGBValues(styleColourString);
  35. }
  36.  
  37. void StyleColour::setRGBValues(const string & styleColourString){
  38.   //Stringstream zum Einlesen der Tokens:
  39.   istringstream valueStream(styleColourString.c_str());
  40.   valueStream >> r;
  41.   valueStream >> g;
  42.   valueStream >> b;
  43. }
  44.  
  45. void StyleColour::setRedValue(const string & r_hex)
  46. {
  47.   r = r_hex;
  48. }
  49.  
  50. void StyleColour::setGreenValue(const string & g_hex)
  51. {
  52.   g = g_hex;
  53. }
  54.  
  55. void StyleColour::setBlueValue(const string & b_hex)
  56. {
  57.   b = b_hex;
  58. }
  59.  
  60. string& StyleColour::getHexRedValue()
  61. {
  62.   return r;
  63. }
  64. string& StyleColour::getHexGreenValue()
  65. {
  66.   return g;
  67. }
  68. string& StyleColour::getHexBlueValue()
  69. {
  70.   return b;
  71. }
  72.  
  73.  
  74. string StyleColour::getRTFRedValue()
  75. {
  76.   return int2str(hex2dec(r));
  77. }
  78. string StyleColour::getRTFGreenValue()
  79. {
  80.   return int2str(hex2dec(g));
  81. }
  82. string StyleColour::getRTFBlueValue()
  83. {
  84.   return int2str(hex2dec(b));
  85. }
  86.  
  87.  
  88. string StyleColour::getLatexRedValue()
  89. {
  90.   return float2str((float)hex2dec(r)/255);
  91. }
  92. string StyleColour::getLatexGreenValue()
  93. {
  94.   return float2str((float)hex2dec(g)/255);
  95. }
  96. string StyleColour::getLatexBlueValue()
  97. {
  98.   return float2str((float)hex2dec(b)/255);
  99. }
  100.  
  101. // Konvertieren von RGB nach CYM
  102. string StyleColour::getTexRedValue()
  103. {
  104.   return float2str(1-(float)hex2dec(r)/255);
  105. }
  106. string StyleColour::getTexGreenValue()
  107. {
  108.   return float2str(1-(float)hex2dec(g)/255);
  109. }
  110. string StyleColour::getTexBlueValue()
  111. {
  112.   return float2str(1-(float)hex2dec(b)/255);
  113. }
  114.  
  115.  
  116. string StyleColour::int2str(const int num)
  117. {
  118.   std::ostringstream outStream;
  119.   outStream << num;
  120.  
  121.   return outStream.str();
  122. }
  123.  
  124. string StyleColour::float2str(const double num)
  125. {
  126.   std::ostringstream outStream;
  127.   outStream << ( floor ( num * 100 + .5 ) / 100);
  128.  
  129.   return outStream.str();
  130. }
  131.  
  132. int  StyleColour::hex2dec(const string &hexVal)
  133. {
  134.  
  135.   if (hexVal.length() != 2)
  136.     return 0;
  137.  
  138.   unsigned int decVal=0, koeff=16;
  139.  
  140.   for (int i=0; i<2;i++ )
  141.     {
  142.       if ((hexVal[i] >= '0')&& (hexVal[i]<= '9' ))
  143.         {
  144.           decVal += (koeff * (hexVal[i]-'0'));
  145.  
  146.         }
  147.       if ((hexVal[i] >= 'a')&& (hexVal[i]<= 'f' ))
  148.         {
  149.           decVal +=( koeff * (hexVal[i]-87));
  150.         }
  151.       if ((hexVal[i] >= 'A')&& (hexVal[i]<= 'F' ))
  152.         {
  153.           decVal += (koeff * (hexVal[i]-55));
  154.         }
  155.       koeff=1;
  156.     }
  157.   return decVal;
  158. }
  159.  
  160. }
  161.  
  162.